//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Reader.Components;

namespace CommunityServer.ControlPanel.Reader
{
	/// <summary>
	/// Summary description for FeedEditor.
	/// </summary>
	public class FeedEditor : BaseReaderAdminPage
	{
		protected System.Web.UI.WebControls.Literal lFeedId;
		protected System.Web.UI.WebControls.TextBox txtUrl;
		protected System.Web.UI.WebControls.Literal lTitle;
		protected System.Web.UI.WebControls.Literal lLink;
		protected System.Web.UI.WebControls.Literal lLanguage;
		protected System.Web.UI.WebControls.Literal lGenerator;
		protected System.Web.UI.WebControls.Literal lSubscribeDate;
		protected System.Web.UI.WebControls.Literal lLastUpdateDate;
		protected System.Web.UI.WebControls.Literal lLastModified;
		protected System.Web.UI.WebControls.Literal lETag;
		protected System.Web.UI.WebControls.Literal lFeedState;
		protected ResourceLinkButton SaveButton;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				LoadFeedInformation();
			}
		}

		private void LoadFeedInformation()
		{
			int feedId = int.Parse(Request.QueryString["FeedId"]);


			Feed feed = Feeds.GetFeed(feedId);

			lFeedId.Text			= feed.FeedId.ToString();
			txtUrl.Text				= feed.Url;
			lTitle.Text				= feed.Title;
			lLink.Text				= feed.Link;
			lLanguage.Text			= feed.Language;
			lGenerator.Text			= feed.Generator;
			lSubscribeDate.Text		= feed.SubscribeDate.ToString();
			lLastUpdateDate.Text	= feed.LastUpdateDate.ToString();
			lLastModified.Text		= feed.LastModified.ToString();
			lETag.Text				= feed.ETag;
			lFeedState.Text			= feed.FeedState.ToString();

			ViewState["FeedStateId"] = feed.FeedStateId;

		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);
			SaveButton.Click += new EventHandler(SaveButton_Click);

		}
		#endregion

		private void SaveButton_Click(object sender, EventArgs e)
		{
			Feed feed = new Feed();


			feed.FeedId			= int.Parse(lFeedId.Text);
			feed.Url			= txtUrl.Text;
			feed.Title			= lTitle.Text;
			feed.Link			= lLink.Text;
			feed.Language		= lLanguage.Text;
			feed.Generator		= lGenerator.Text;
			feed.SubscribeDate	= DateTime.Parse(lSubscribeDate.Text);
			feed.LastUpdateDate = DateTime.Parse(lLastUpdateDate.Text);
			feed.LastModified	= DateTime.Parse(lLastModified.Text);
			feed.ETag			= lETag.Text;
			feed.FeedStateId	= int.Parse(ViewState["FeedStateId"].ToString());

			// Update the feed.
			Feeds.UpdateFeed(feed);

			// Redirect.
			Response.Redirect("ManageFeeds.aspx");

		}

	}
}
